package com.szholly.data.general;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.szholly.utils.util.StringUtils;
import com.szholly.data.common.util.ValueConvertUtils;

/**
 * 类功能说明 定义数据值转换类
 */
public class DbValueConvertUtils {

	/**
	 * @Title: ConvertValue
	 * @Description: 转换数据值
	 * @param @param fm
	 * @param @param fieldName
	 * @param @param obj
	 * @param @return
	 * @return Object 返回类型
	 * @throws
	 */
	public static Object ConvertValue(GlobalDataType fm, String fieldName,
			Object obj) {
		if (null == obj || "".equals(obj))
			return null;

		String strObj = obj.toString();
		if ("null".equals(strObj)) {
			return null;
		}
		Object value = null;
		switch (fm) {
		case Boolean:
			if (obj instanceof Boolean)
				value = obj;
			else {
				strObj = getBoolValue(strObj);
				boolean b = false;
				try {
					b = Boolean.parseBoolean(strObj);
					value = b;
				} catch (Exception e) {
				}

			}
			break;
		case Char:
			if (obj instanceof Character)
				value = obj;
			else {
				char[] c = strObj.toCharArray();
				value = c;
			}
			break;
		case String:
		case Text:
			value = strObj;
			break;
		case Date:
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			try {
				if ("null".equals(strObj))
					return null;
				strObj = strObj.replace(" 00:00:00", "");
				// 处理如果类型为2013-12没有日期的类型 : 陈旭 2013.12.24
				String[] strObjs = strObj.split("-");
				if (strObjs.length == 2) {
					strObj = strObj + "-01";
				}
				value = dateFormat.parse(strObj);
				// 对转换后日期进行yyyy-MM-dd格式化：荣超 2014.04.28
				value = dateFormat.format(value);
			} catch (Exception e1) {
				value = obj;
				e1.printStackTrace();
			}
			break;
		case DateTime:
			dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				value = dateFormat.parse(strObj);
			} catch (ParseException e1) {
				value = obj;
				e1.printStackTrace();
			}
			break;
		case Decimal:
			if (obj instanceof BigDecimal)
				value = obj;
			else {
				strObj = getDigitValue(strObj);
				BigDecimal dl = new BigDecimal(strObj);
				value = dl;
			}
			break;
		case Double:
			if (obj instanceof Double)
				value = obj;
			else {
				strObj = getDigitValue(strObj);
				Double de = 0d;
				try {
					de = Double.parseDouble(strObj);
					value = de;
				} catch (Exception e) {
				}
			}
			break;
		case Single:
			// break;
		case Int16:
			if (obj instanceof Integer)
				value = obj;
			else {
				strObj = getDigitValue(strObj);
				try {
					value = Integer.parseInt(strObj);
				} catch (Exception e) {

				}
			}
			break;
		case Int32:
			if (obj instanceof Integer)
				value = obj;
			else {
				strObj = getDigitValue(strObj);
				try {
					value = Integer.parseInt(strObj);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			break;
		case Int64:
			if (obj instanceof Float)
				value = obj;
			else {
				strObj = getDigitValue(strObj);
				value = Float.parseFloat(strObj);
			}
			break;
		case UInt16:
			if (obj instanceof Integer)
				value = obj;
			else {
				strObj = getDigitValue(strObj);
				try {
					value = Integer.parseInt(strObj);
				} catch (Exception e) {

				}
			}
			break;
		case UInt32:
			if (obj instanceof Integer)
				value = obj;
			else {
				strObj = getDigitValue(strObj);
				try {
					value = Integer.parseInt(strObj);
				} catch (Exception e) {

				}
			}
			break;
		case UInt64:
			if (obj instanceof Integer)
				value = obj;
			else {
				strObj = getDigitValue(strObj);
				try {
					value = Integer.parseInt(strObj);
				} catch (Exception e) {

				}
			}
			break;
		case ByteArray:
			value = obj;
			break;
		case NoDefault:
			break;
		default:
			break;
		}
		return value;
	}

	static String getDigitValue(String strObj) {
		if (strObj.toUpperCase() == "TRUE" || strObj == "是")
			return "1";
		else if (strObj.toUpperCase() == "FALSE" || strObj == "否")
			return "0";
		else
			return strObj;
	}

	static String getBoolValue(String strObj) {
		if (strObj == "1" || strObj == "是" || strObj.toUpperCase() == "Yes")
			return "True";
		else if (strObj == "0" || strObj == "否" || strObj.toUpperCase() == "No")
			return "False";
		else
			return strObj;
	}

	/**
	 * @Title: GetRowObject
	 * @Description: 从DataRow中获取指定字段值
	 * @param @param dr
	 * @param @param fieldName
	 * @param @return
	 * @return Object 返回类型
	 * @throws
	 */
	public static Object GetRowObject(RowBase dr, String fieldName) {
		if (dr == null || StringUtils.IsNullOrSpace(fieldName))
			return null;

		Object obj = dr.get(fieldName);
		obj = ValueConvertUtils.ConvertValue(null, obj, obj);
		if (obj == null)
			return null;
		else
			return obj;
	}

	/**
	 * @Title: ConvertToCaption
	 * @Description: 转换为显示值
	 * @param @param dataType
	 * @param @param value
	 * @param @return
	 * @return String 返回类型
	 * @throws
	 */
	public static String ConvertToCaption(GlobalDataType dataType, Object value) {
		if (value == null)
			return "";

		switch (dataType) {
		case Boolean:
		case Char:
		case String:
		case Text:
			return "'" + value.toString() + "'";
		case Date:
		case DateTime:
			return "'" + value.toString() + "'";
		case Decimal:
		case Double:
		case Single:
		case Int16:
		case Int32:
		case Int64:
		case UInt16:
		case UInt32:
		case UInt64:
			return value.toString();
		case ByteArray:
		case NoDefault:
			return "";
		}
		return "";
	}

	/**
	 * @Title: ConvertSearchValue
	 * @Description: 转换查询值(如：如果是字符类型，前后添加‘)
	 * @param @param field
	 * @param @param value
	 * @param @param fieldName
	 * @param @return
	 * @return String 返回类型
	 * @throws
	 */
	public static String ConvertSearchValue(Field field, Object value,
			String fieldName) {
		if (field == null)
			return "";

		if (StringUtils.IsNullOrEmpty(fieldName))
			fieldName = field.getFieldName();

		String strValue = "";
		switch (field.getDataType()) {
		case Char:
		case String:
		case Text:
			strValue = getStrSearchValue(fieldName, value);
			break;
		case Date:
		case DateTime:
			strValue = getStrSearchValue(fieldName, value);
			break;
		case Boolean:
		case Decimal:
		case Double:
		case Single:
		case Int16:
		case Int32:
		case Int64:
		case UInt16:
		case UInt32:
		case UInt64:
			strValue = getIntSearchValue(fieldName, value);
			break;
		case ByteArray:
		case NoDefault:
			break;
		default:
			break;
		}
		return strValue;
	}

	private static String getIntSearchValue(String fieldName, Object value) {
		String strValue = "";
		if (value == null)
			strValue = fieldName + " is null ";
		else if (StringUtils.IsNullOrSpace(value.toString()))
			strValue = "(" + fieldName + " is null or " + fieldName + " = 0 ) ";
		else
			strValue = fieldName + " = " + value.toString();
		return strValue;
	}

	private static String getStrSearchValue(String fieldName, Object value) {
		String strValue = "";
		if (value == null)
			strValue = fieldName + " is null ";
		else if (StringUtils.IsNullOrSpace(value.toString()))
			strValue = "(" + fieldName + " is null or " + fieldName
					+ " = '' ) ";
		else
			strValue = fieldName + " = '" + value.toString() + "' ";
		return strValue;
	}
}
